﻿using System;
using System.Threading;


// 定义闹钟类
public class AlarmClock
{
    public delegate void TickHandler(object sender, EventArgs e);
    public event TickHandler Tick;

    public delegate void AlarmHandler(object sender, EventArgs e);
    public event AlarmHandler Alarm;

    private Timer timer;
    private DateTime alarmTime;

    public AlarmClock(DateTime alarmTime)
    {
        this.alarmTime = alarmTime;
        //1000ms触发一次，且刚开始不触发
        timer = new Timer(OnTimedEvent, null, 1000, 1000);
    }

    private void OnTimedEvent(Object source)
    {
        // 触发嘀嗒事件
        OnTick();

        // 检查是否到达响铃时间
        if (DateTime.Now >= alarmTime)
        {
            // 触发响铃事件
            OnAlarm();
            // 停止定时器
            timer.Dispose();
        }
    }

    protected virtual void OnTick()
    {
        Tick?.Invoke(this, EventArgs.Empty);
    }

    protected virtual void OnAlarm()
    {
        Alarm?.Invoke(this, EventArgs.Empty);
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine($"请输入定时的时间，单位为s");
        string input = Console.ReadLine();
        int x = int.Parse(input);
        DateTime alarmTime = DateTime.Now.AddSeconds(x);
        Console.WriteLine($"闹钟已设置为 {alarmTime} 响铃。");

        // 创建闹钟对象
        AlarmClock clock = new AlarmClock(alarmTime);

        clock.Tick += (sender, e) =>
        {
            Console.WriteLine("嘀嗒");
        };

        clock.Alarm += (sender, e) =>
        {
            Console.WriteLine("闹钟响了");
        };

        // 保持控制台窗口打开
        Console.ReadLine();
    }
}
